package net.lee.springbootvertx.vertx.config;


import io.vertx.rxjava.core.AbstractVerticle;
import io.vertx.rxjava.core.http.HttpServer;
import io.vertx.rxjava.ext.web.Router;
import net.lee.springbootvertx.vertx.route.RouteMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;



@Configuration
public class VertxStart extends AbstractVerticle  {


    @Autowired
    RouteMapping routeMapping;

    @Value("${spring.boot.vertx.server.port}")
    private Integer port;

    public VertxStart()
    {
        if(port==null|| port ==0)
        {
            port = 8080;
        }
    }

    @Override
    public void start() throws Exception {
         routeMapping.init(vertx);
        Router route = routeMapping.getRouter();
        HttpServer httpServer = vertx.createHttpServer();
        httpServer.requestStream().toObservable()
                .subscribe(route::accept);
        httpServer.rxListen(port).subscribe();
    }


}
